

		
	*===================*
	* balance on students
	*===================*
	global subjectvar __tmp_subject1 __tmp_subject2
	
	preserve
	
		duplicates drop stu_id id year, force
		gen _insample = insample
		quiet{
			* Create exp_capped categories
			tab subject, gen(__tmp_subject)
			

			label values ntc1_cat threatweeks
			label values ntc2_cat threatweeks
			label values ntc3_cat threatweeks
			label variable experience "Teacher Experience"
			
			
			label variable male "Male"
			label variable black "Black"
			label variable hisp "Hispanic"
			label variable othrace "Other Race"
			label variable lep_dv "English Learner"
			label variable sped_dv "Special Education"
			label variable math_std_t "Previous Math"
			label variable read_std_t "Previous Reading"
			label variable frpl_dv "Free or Reduced Price Lunch"
			label variable exp_capped "Teacher Experience"
			label variable exp_capped_squared "Teacher Experience Squared"
			
			
			
			}
		
		
				
		forvalue j=1/2 {
			if `j' == 1 {
				local mylist nothreat_cycle1 nothreat_cycle2 nothreat_cycle3 nothreat_p2 nothreat_m2 nothreat_m1
			}
			else if `j' == 2 {
				local mylist  diff_m1 diff_m2 diff_p1 diff_p2
				
			}
			
			
			foreach var of varlist `mylist' {
				quiet{
					if "`var'" == "nothreat_m1"{
							replace _insample = 0 if year == 2010
					}
					else {
						replace _insample = insample
					}
					estimates clear
					local i = 1
				
				
					quiet: reghdfe `var' $experiencevar $sdemovars_race $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars_race 
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1
					
					quiet: reghdfe `var' $experiencevar $sdemovars $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1
					
					quiet: reghdfe `var' $experiencevar $prevvars $sdemovars $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars $prevvars
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1				
					
					* prepare estimates for estout
					estfe model*, labels(stu_id "Student FE" sch_id "School FE" year "Year FE" grade "Grade FE" id "Teacher FE")
					return list
					}
					
				*
				esttab model* using "$output_dir\Tables\_Table_13_`var'.tex",  replace indicate(`r(indicate_fe)' "Subject FE=__tmp_subject*" , labels("X" " ")) ///
					nobaselevels star(+ 0.10 * 0.05 ** 0.01 *** 0.001 ) noconstant noomitted nomtitle scalars("F_diff F" "p_diff p") se label ///
					drop(year_dose experience experience_squared _cons) nonotes substitute(/ "")
				
				* return stored estimates to their previous state
				estfe model*, restore
				}
			}
			
			* create one combined table for p2 and m2
				estimates clear
				
					* p2
					local i = 1
					quiet: reghdfe nothreat_p2 $experiencevar $prevvars $sdemovars $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars $prevvars
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1				
					
					quiet: reghdfe nothreat_m2 $experiencevar $prevvars $sdemovars $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars $prevvars
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1				
					
					quiet: reghdfe nothreat_cycle3 $experiencevar $prevvars $sdemovars $subjectvar year_dose if _insample, absorb(sch_id year) cluster(id#year)
					test $experiencevar $sdemovars $prevvars
					estadd scalar F_diff = r(F)
					estadd scalar p_diff = r(p)
					estimates store model_`i'
					local i = `i' + 1				
					
									
					* prepare estimates for estout
					estfe model*, labels(stu_id "Student FE" sch_id "School FE" year "Year FE" grade "Grade FE" id "Teacher FE")
					return list
					
					
				*"p(par(zz ww) fmt(%5.4fc)  label(!))"
				esttab model* using "$output_dir\Tables\_Table_13_combined_p2m2nothreat3.tex",  replace indicate(`r(indicate_fe)' "Subject FE=__tmp_subject*" , labels("X" " ")) ///
					mtitles("Admin" "External" "Unmonitored Time") nobaselevels noconstant noomitted scalars("F_diff F" "p_diff p") se label ///
					cells("b(${stars} fmt(%5.4fc) label(!))" "se(par fmt(%5.4fc) label(!))" ) ///
					substitute(zz "[" qq " " ww "]" ! " " / "") gaps starlevels(\$^+\$ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
					drop(experience_squared _cons year_dose) nonotes
				
				* return stored estimates to their previous state
				estfe model*, restore
			
			
			
			
			
			
		restore
		
		